home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / modes / asm-mode.el.z / asm-mode.el
Encoding:
Text File  |  1998-05-21  |  8.5 KB  |  262 lines

  1. ;;; asm-mode.el --- mode for editing assembler code
  2.  
  3. ;; Copyright (C) 1991 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
  6. ;; Maintainer: FSF
  7. ;; Keywords: tools, languages
  8.  
  9. ;; This file is part of XEmacs.
  10.  
  11. ;; XEmacs is free software; you can redistribute it and/or modify it
  12. ;; under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; XEmacs is distributed in the hope that it will be useful, but
  17. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19. ;; General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  23. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  24. ;; 02111-1307, USA.
  25.  
  26. ;;; Synched up with: FSF 19.34.
  27.  
  28. ;;; Commentary:
  29.  
  30. ;; This mode was written by Eric S. Raymond <esr@snark.thyrsus.com>,
  31. ;; inspired by an earlier asm-mode by Martin Neitzel.
  32.  
  33. ;; This minor mode is based on text mode.  It defines a private abbrev table
  34. ;; that can be used to save abbrevs for assembler mnemonics.  It binds just
  35. ;; five keys:
  36. ;;
  37. ;;    TAB        tab to next tab stop
  38. ;;    :        outdent preceding label, tab to tab stop
  39. ;;    comment char    place or move comment
  40. ;;            asm-comment-char specifies which character this is;
  41. ;;            you can use a different character in different
  42. ;;            Asm mode buffers.
  43. ;;    C-j, C-m    newline and tab to tab stop
  44. ;;
  45. ;; Code is indented to the first tab stop level.
  46.  
  47. ;; This mode runs two hooks:
  48. ;;   1) An asm-mode-set-comment-hook before the part of the initialization
  49. ;; depending on asm-comment-char, and
  50. ;;   2) an asm-mode-hook at the end of initialization.
  51.  
  52. ;;; Code:
  53.  
  54. (defgroup asm nil
  55.   "Assembler programming"
  56.   :group 'languages)
  57.  
  58.  
  59. (defcustom asm-comment-char ?;
  60.   "*The comment-start character assumed by Asm mode."
  61.   :type 'sexp
  62.   :group 'asm)
  63.  
  64. ;; XEmacs change (This is the primary difference, why was this
  65. ;;  feature removed? -sb)
  66. (defcustom asm-support-c-comments-p t
  67.   "*Support C style comments.  If t C style comments will be
  68. supported.  This is mainly for the benefit of font-lock."
  69.   :type 'boolean
  70.   :group 'asm)
  71.  
  72. (defcustom asm-mode-syntax-table nil
  73.   "Syntax table used while in Asm mode.")
  74.  
  75. (defvar asm-mode-abbrev-table nil
  76.   "Abbrev table used while in Asm mode.")
  77. (define-abbrev-table 'asm-mode-abbrev-table ())
  78.  
  79. (defvar asm-mode-map nil
  80.   "Keymap for Asm mode.")
  81.  
  82. (if asm-mode-map
  83.     nil
  84.   ;; XEmacs change
  85.   (setq asm-mode-map (make-sparse-keymap 'asm-mode-map))
  86.   ;; Note that the comment character isn't set up until asm-mode is called.
  87.   (define-key asm-mode-map ":"        'asm-colon)
  88.   (define-key asm-mode-map "\C-c;"    'comment-region)
  89.   (define-key asm-mode-map "\C-i"    'tab-to-tab-stop)
  90.   (define-key asm-mode-map "\C-j"    'asm-newline)
  91.   (define-key asm-mode-map "\C-m"    'asm-newline)
  92.   )
  93.  
  94. (defconst asm-font-lock-keywords
  95.  '(("^\\(\\(\\sw\\|\\s_\\)+\\)\\>:?[ \t]*\\(\\sw+\\)?"
  96.     (1 font-lock-function-name-face) (3 font-lock-keyword-face nil t))
  97.    ("^\\s +\\(\\(\\sw\\|\\s_\\)+\\)" 1 font-lock-keyword-face))
  98.  "Additional expressions to highlight in Assembler mode.")
  99.  
  100. ;; XEmacs change
  101. (put 'asm-mode 'font-lock-defaults '(asm-font-lock-keywords))
  102. (defvar asm-code-level-empty-comment-pattern nil)
  103. (defvar asm-flush-left-empty-comment-pattern nil)
  104. (defvar asm-inline-empty-comment-pattern nil)
  105.  
  106. ;;;###autoload
  107. (defun asm-mode ()
  108.   "Major mode for editing typical assembler code.
  109. Features a private abbrev table and the following bindings:
  110.  
  111. \\[asm-colon]\toutdent a preceding label, tab to next tab stop.
  112. \\[tab-to-tab-stop]\ttab to next tab stop.
  113. \\[asm-newline]\tnewline, then tab to next tab stop.
  114. \\[asm-comment]\tsmart placement of assembler comments.
  115.  
  116. The character used for making comments is set by the variable
  117. `asm-comment-char' (which defaults to `?;').
  118.  
  119. Alternatively, you may set this variable in `asm-mode-set-comment-hook',
  120. which is called near the beginning of mode initialization.
  121.  
  122. Turning on Asm mode runs the hook `asm-mode-hook' at the end of initialization.
  123.  
  124. Special commands:
  125. \\{asm-mode-map}
  126. "
  127.   (interactive)
  128.   (kill-all-local-variables)
  129.   (setq mode-name "Assembler")
  130.   (setq major-mode 'asm-mode)
  131.   (setq local-abbrev-table asm-mode-abbrev-table)
  132.   (make-local-variable 'font-lock-defaults)
  133.   (setq font-lock-defaults '(asm-font-lock-keywords))
  134.   (make-local-variable 'asm-mode-syntax-table)
  135.   (setq asm-mode-syntax-table (make-syntax-table))
  136.   (set-syntax-table asm-mode-syntax-table)
  137.  
  138.   (run-hooks 'asm-mode-set-comment-hook)
  139.   ;; Make our own local child of asm-mode-map
  140.   ;; so we can define our own comment character.
  141.   ;; XEmacs change
  142.   (let ((ourmap (make-sparse-keymap)))
  143.     (set-keymap-parents ourmap (list asm-mode-map))
  144.     (use-local-map ourmap))
  145.   (local-set-key (vector asm-comment-char) 'asm-comment)
  146.   ;; XEmacs change
  147.   (if asm-support-c-comments-p
  148.       (progn
  149.     (modify-syntax-entry ?/ ". 14" asm-mode-syntax-table)
  150.     (modify-syntax-entry ?* ". 23" asm-mode-syntax-table)
  151.     (modify-syntax-entry asm-comment-char "< b" asm-mode-syntax-table)
  152.     (modify-syntax-entry ?\n "> b" asm-mode-syntax-table))
  153.     (progn
  154.       (modify-syntax-entry asm-comment-char
  155.                "<" asm-mode-syntax-table)
  156.       (modify-syntax-entry ?\n
  157.                ">" asm-mode-syntax-table)))
  158.   (let ((cs (regexp-quote (char-to-string asm-comment-char))))
  159.     (make-local-variable 'comment-start)
  160.     (setq comment-start (concat cs " "))
  161.     (make-local-variable 'comment-start-skip)
  162.     (setq comment-start-skip (concat cs "+[ \t]*"))
  163.     (setq asm-inline-empty-comment-pattern (concat "^.+" cs "+ *$"))
  164.     (setq asm-code-level-empty-comment-pattern (concat "^[\t ]+" cs cs " *$"))
  165.     (setq asm-flush-left-empty-comment-pattern (concat "^" cs cs cs " *$"))
  166.     )
  167.   (make-local-variable 'comment-end)
  168.   (setq comment-end "")
  169.   (make-local-variable 'comment-column)
  170.   (setq comment-column 32)
  171.   (setq fill-prefix "\t")
  172.   (run-hooks 'asm-mode-hook))
  173.  
  174. (defun asm-colon ()
  175.   "Insert a colon; if it follows a label, delete the label's indentation."
  176.   (interactive)
  177.   (save-excursion
  178.     (beginning-of-line)
  179.     (if (looking-at "[ \t]+\\(\\sw\\|\\s_\\)+$")
  180.     (delete-horizontal-space)))
  181.   (insert ":")
  182.   (tab-to-tab-stop)
  183.   )
  184.  
  185. (defun asm-newline ()
  186.   "Insert LFD + fill-prefix, to bring us back to code-indent level."
  187.   (interactive)
  188.   (if (eolp) (delete-horizontal-space))
  189.   (insert "\n")
  190.   (tab-to-tab-stop)
  191.   )
  192.  
  193. (defun asm-line-matches (pattern &optional withcomment)
  194.   (save-excursion
  195.     (beginning-of-line)
  196.     (looking-at pattern)))
  197.  
  198. (defun asm-pop-comment-level ()
  199.   ;; Delete an empty comment ending current line.  Then set up for a new one,
  200.   ;; on the current line if it was all comment, otherwise above it
  201.   (end-of-line)
  202.   (delete-horizontal-space)
  203.   (while (= (preceding-char) asm-comment-char)
  204.     (delete-backward-char 1))
  205.   (delete-horizontal-space)
  206.   (if (bolp)
  207.       nil
  208.     (beginning-of-line)
  209.     (open-line 1))
  210.   )
  211.  
  212.  
  213. (defun asm-comment ()
  214.   "Convert an empty comment to a `larger' kind, or start a new one.
  215. These are the known comment classes:
  216.  
  217.    1 -- comment to the right of the code (at the comment-column)
  218.    2 -- comment on its own line, indented like code
  219.    3 -- comment on its own line, beginning at the left-most column.
  220.  
  221. Suggested usage:  while writing your code, trigger asm-comment
  222. repeatedly until you are satisfied with the kind of comment."
  223.   (interactive)
  224.   (cond
  225.  
  226.    ;; Blank line?  Then start comment at code indent level.
  227.    ((asm-line-matches "^[ \t]*$")
  228.     (delete-horizontal-space)
  229.     (tab-to-tab-stop)
  230.     (insert asm-comment-char comment-start))
  231.  
  232.    ;; Nonblank line with no comment chars in it?
  233.    ;; Then start a comment at the current comment column
  234.    ((asm-line-matches (format "^[^%c\n]+$" asm-comment-char))
  235.     (indent-for-comment))
  236.  
  237.    ;; Flush-left comment present?  Just insert character.
  238.    ((asm-line-matches asm-flush-left-empty-comment-pattern)
  239.     (insert asm-comment-char))
  240.  
  241.    ;; Empty code-level comment already present?
  242.    ;; Then start flush-left comment, on line above if this one is nonempty. 
  243.    ((asm-line-matches asm-code-level-empty-comment-pattern)
  244.     (asm-pop-comment-level)
  245.     (insert asm-comment-char asm-comment-char comment-start))
  246.  
  247.    ;; Empty comment ends line?
  248.    ;; Then make code-level comment, on line above if this one is nonempty. 
  249.    ((asm-line-matches asm-inline-empty-comment-pattern)
  250.     (asm-pop-comment-level)
  251.     (tab-to-tab-stop)
  252.     (insert asm-comment-char comment-start))
  253.  
  254.    ;; If all else fails, insert character
  255.    (t
  256.     (insert asm-comment-char))
  257.  
  258.    )
  259.   (end-of-line))
  260.  
  261. ;;; asm-mode.el ends here
  262.